# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

  include SslRequirement # For SSL, Plugin By DHH
  helper :date

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password

  include AuthenticatedSystem
  # You can move this into a different controller, if you wish.  This module gives you the require_role helpers, and others.
  include RoleRequirementSystem

  # Scrub sensitive parameters from your log
  filter_parameter_logging :password

  # Mix in the methods from the ApplicationHelper module
  include ApplicationHelper

  before_filter :init

  # Return true if a parameter corresponding to the given symbol was posted.


  def init
    @content_types = ContentType.find(:all, :order => "id")

    if is_editor?
      @edit_flag = true
    else
      @edit_flag = false
    end
  end

  def param_posted?(sym)
    request.post? and params[sym]
  end

  def ssl_required?
    return false if RAILS_ENV == 'development' || RAILS_ENV == 'test'
    super
  end

  def require_user
    return true
  end

  def is_editor?
    if logged_in?
      u = User.find current_user.id
      if u.nil?
        return false
      else
        myroles = u.roles
        myroles.each do |m|
          if m.name.downcase == "editor"
            return true
          end
        end
      end
    end
    return false
  end

  def render_404(exception = nil)
    if exception
      logger.info "Rendering 404 with exception: #{exception.message}"
    end

    respond_to do |format|
      format.html { render :file => "#{Rails.root}/public/404.html", :status => :not_found }
      format.xml  { head :not_found }
      format.any  { head :not_found }
    end
  end



  #def current_user
  #return current_user
  #end
end
